<template>
  <div class="container">
    <el-dialog
      :title="title"
      :visible="showDialog"
      width="25%"
      @close="closeAddDialog"
    >
      <el-form
        :model="form"
        label-width="80px"
        :rules="rules"
        ref="form"
      >
        <el-form-item
          label="所属学科"
          aria-placeholder="请选择"
          v-if="!$route.query.id"
        >
          <el-select
            class="filter-item"
            v-model="form.subjectID"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in subjectNameList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>

          </el-select>
        </el-form-item>
        <el-form-item
          label="标签名称"
          prop="tagName"
          style="width: 100%"
        >
          <el-input
            v-model="form.tagName"
            autocomplete="off"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="closeAddDialog">取 消</el-button>
        <el-button
          type="primary"
          @click="addTags"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { subjectSimpleAPI } from '@/api/hmmm/subjects.js'
import { labelAddAPI, labelUpdateAPI } from '@/api/hmmm/tags.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        tagName: '',
        subjectID: ''
      },
      query: {
        page: 1,
        pagesize: 100
      },
      subjectNameList: [],
      rules: {
        tagName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      },
      title: ''
    }
  },
  mounted() {
    this.getList()
  },

  methods: {
    async getList() {
      const res = await subjectSimpleAPI()
      console.log(res)
      this.subjectNameList = res.data
    },
    // 兜底校验  wo
    addTags() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            // 修改
            await labelUpdateAPI(this.form)
            this.$message.success('修改成功')
          } else {
            if (this.$route.query.id) {
              this.form.subjectID = Number(this.$route.query.id)
            }
            // 添加
            await labelAddAPI(this.form)
            this.$message.success('添加成功')
          }
          // 清空验证
          this.$refs.form.resetFields()

          // 父传子
          this.$emit('getList')
          // 清空内容
          this.form = {
            tagName: '',
            subjectID: ''
          }
          this.closeAddDialog()
        }
      })
    },
    closeAddDialog() {
      this.$emit('close-dialog')
    }
  }
}
</script>
<style scoped lang='less'></style>
